#ifndef CDEAD6809
#define CDEAD6809 CDead6809
#endif
__inline void CDEAD6809::Indexed()
{
u16 *Dest,t;
u8 Byte;

Byte = Read(PC++);
switch((Byte >> 5) & 3)
	{
	default:
	case 0:	Dest = &X;	break;
	case 1:	Dest = &Y;	break;
	case 2:	Dest = &U;	break;
	case 3:	Dest = &S;	break;
	}
if((Byte & 0x80) == 0)
	{
	if(Byte & 0x10)		//negative
		EA = *Dest - (0x10 - (Byte & 0xF));
	else						//positive
		EA = *Dest + (Byte & 0xF);
	Cycle++;
	return;
	}
switch(Byte & 0xF)
	{
	default:
		message("indexed addressing...bad mode index ($%02X)...\n",Byte);
		break;
	case 0x0:
		EA = *Dest;
		*Dest += 1;
		Cycle += 2;
		break;
	case 0x1:
		EA = *Dest;
		*Dest += 2;
		Cycle += 3;
		break;
	case 0x2:
		*Dest -= 1;
		EA = *Dest;
		Cycle += 2;
		break;
	case 0x3:
		*Dest -= 2;
		EA = *Dest;
		Cycle += 2;
		break;
	case 0x4:
		EA = *Dest;
		break;
	case 0x5:
		EA = (u16)(*Dest + (s16)(s8)B);
		Cycle++;
		break;
	case 0x6:
		EA = (u16)(*Dest + (s16)(s8)A);
		//message("idx $%02X, EA = $%04X\n",Byte,EA);
		Cycle++;
		break;
	case 0x8:
		//EA = (u16)(*Dest + (s16)(s8)Read(PC++));		//correct?
		t = Read(PC++);
		if(t & 0x80)
			t |= 0xFF00;
		EA = (u16)(*Dest + t);
		Cycle++;
		break;
	case 0x9:
		EA = (u16)(*Dest + ReadWord(PC));
		PC += 2;
		Cycle += 4;
		break;
	case 0xB:
		EA = (u16)(*Dest + D);
		Cycle += 4;
		break;
	case 0xC:
		EA = Read(PC++);
		EA += PC;
		Cycle += 1;
		break;
	case 0xD:
		EA = ReadWord(PC);
		PC += 2;
		EA += PC;
		Cycle += 5;
		break;
	case 0xF:
		EA = ReadWord(PC);
		PC += 2;
		Cycle += 5;
		break;
	}
if(Byte & 0x10)
	{
	EA = ReadWord(EA);
	Cycle += 3;
	}
}

#define PUSHBYTE(b) --S; WM(SD,b)
#define PUSHWORD(w) --S; WM(SD,w.b.l); --S; WM(SD,w.b.h)
#define PULLBYTE(b) b = RM(SD); S++
#define PULLWORD(w) w = RM(SD)<<8; S++; w |= RM(SD); S++

__inline void CDEAD6809::PushS(u8 data)
{
S--;
Write(S,data);
}

__inline void CDEAD6809::PushU(u8 data)
{
U--;
Write(U,data);
}

__inline void CDEAD6809::PushS16(u16 data)
{
PushS((u8)data);
PushS((u8)(data >> 8));
}

__inline void CDEAD6809::PushU16(u16 data)
{
PushU((u8)data);
PushU((u8)(data >> 8));
}

__inline u8 CDEAD6809::PullS()
{
u8 ret = Read(S);

S += 1;
return(ret);
}

__inline u8 CDEAD6809::PullU()
{
u8 ret = Read(U);

U += 1;
return(ret);
}

__inline u16 CDEAD6809::PullS16()
{
u16 ret = ReadWord(S);

S += 2;
return(ret);
}

__inline u16 CDEAD6809::PullU16()
{
u16 ret = ReadWord(U);

U += 2;
return(ret);
}

__inline void CDEAD6809::PSHS()
{
u8 Byte = Read(PC++);

if(Byte & 0x80)
	{
	PushS16(PC);
	Cycle += 2;
	}
if(Byte & 0x40)
	{
	PushS16(U);
	Cycle += 2;
	}
if(Byte & 0x20)
	{
	PushS16(Y);
	Cycle += 2;
	}
if(Byte & 0x10)
	{
	PushS16(X);
	Cycle += 2;
	}
if(Byte & 8)
	{
	PushS(DP);
	Cycle++;
	}
if(Byte & 4)
	{
	PushS(B);
	Cycle++;
	}
if(Byte & 2)
	{
	PushS(A);
	Cycle++;
	}
if(Byte & 1)
	{
	PushS(CC);
	Cycle++;
	}
}

__inline void CDEAD6809::PSHU()
{
u8 Byte = Read(PC++);

if(Byte & 0x80)
	{
	PushU16(PC);
	Cycle += 2;
	}
if(Byte & 0x40)
	{
	PushU16(S);
	Cycle += 2;
	}
if(Byte & 0x20)
	{
	PushU16(Y);
	Cycle += 2;
	}
if(Byte & 0x10)
	{
	PushU16(X);
	Cycle += 2;
	}
if(Byte & 8)
	{
	PushU(DP);
	Cycle++;
	}
if(Byte & 4)
	{
	PushU(B);
	Cycle++;
	}
if(Byte & 2)
	{
	PushU(A);
	Cycle++;
	}
if(Byte & 1)
	{
	PushU(CC);
	Cycle++;
	}
}

__inline void CDEAD6809::PULS()
{
u8 Byte = Read(PC++);

if(Byte & 1)
	{
	CC = PullS();
	Cycle++;
	}
if(Byte & 2)
	{
	A = PullS();
	Cycle++;
	}
if(Byte & 4)
	{
	B = PullS();
	Cycle++;
	}
if(Byte & 8)
	{
	DP = PullS();
	Cycle++;
	}
if(Byte & 0x10)
	{
	X = PullS16();
	Cycle += 2;
	}
if(Byte & 0x20)
	{
	Y = PullS16();
	Cycle += 2;
	}
if(Byte & 0x40)
	{
	U = PullS16();
	Cycle += 2;
	}
if(Byte & 0x80)
	{
	PC = PullS16();
	Cycle += 2;
	}
}

__inline void CDEAD6809::PULU()
{
u8 Byte = Read(PC++);

if(Byte & 1)
	{
	CC = PullU();
	Cycle++;
	}
if(Byte & 2)
	{
	A = PullU();
	Cycle++;
	}
if(Byte & 4)
	{
	B = PullU();
	Cycle++;
	}
if(Byte & 8)
	{
	DP = PullU();
	Cycle++;
	}
if(Byte & 0x10)
	{
	X = PullU16();
	Cycle += 2;
	}
if(Byte & 0x20)
	{
	Y = PullU16();
	Cycle += 2;
	}
if(Byte & 0x40)
	{
	S = PullU16();
	Cycle += 2;
	}
if(Byte & 0x80)
	{
	PC = PullU16();
	Cycle += 2;
	}
}

static u8 tD8,tS8 = 0;
static u16 tD16,tS16 = 0;

__inline void CDEAD6809::EXG()
{
u8 Byte;
u8 SrcReg,DestReg;
u8 *STemp8 = &tS8,*DTemp8 = &tD8;
u16 *STemp16 = &tS16,*DTemp16 = &tD16;

Byte = Read(PC++);
SrcReg = (Byte >> 4) & 0xF;
DestReg = Byte & 0xF;
switch(SrcReg)
	{
	case 0:	STemp16 = &D;	break;
	case 1:	STemp16 = &X;	break;
	case 2:	STemp16 = &Y;	break;
	case 3:	STemp16 = &U;	break;
	case 4:	STemp16 = &S;	break;
	case 5:	STemp16 = &PC;	break;
	case 0x8:STemp8 = &A;	break;
	case 0x9:STemp8 = &B;	break;
	case 0xA:STemp8 = &CC;	break;
	case 0xB:STemp8 = &DP;	break;
	}
switch(DestReg)
	{
	case 0:	DTemp16 = &D;	break;
	case 1:	DTemp16 = &X;	break;
	case 2:	DTemp16 = &Y;	break;
	case 3:	DTemp16 = &U;	break;
	case 4:	DTemp16 = &S;	break;
	case 5:	DTemp16 = &PC;	break;
	case 0x8:DTemp8 = &A;	break;
	case 0x9:DTemp8 = &B;	break;
	case 0xA:DTemp8 = &CC;	break;
	case 0xB:DTemp8 = &DP;	break;
	}
if(DestReg & 8)	//8 bit value
	{
	u8 Temp8;
	
	Temp8 = *STemp8;
	*STemp8 = *DTemp8;
	*DTemp8 = Temp8;
	}
else					//16 bit value
	{
	u16 Temp16;
	
	Temp16 = *STemp16;
	*STemp16 = *DTemp16;
	*DTemp16 = Temp16;
	}
}

__inline void CDEAD6809::TFR()
{
u8 Byte;
u8 SrcReg,DestReg;
u32 Temp = 0;

Byte = Read(PC++);
SrcReg = (Byte >> 4) & 0xF;
DestReg = Byte & 0xF;
switch(SrcReg)
	{
	case 0:	Temp = D;	break;
	case 1:	Temp = X;	break;
	case 2:	Temp = Y;	break;
	case 3:	Temp = U;	break;
	case 4:	Temp = S;	break;
	case 5:	Temp = PC;	break;
	case 0x8:Temp = A;	break;
	case 0x9:Temp = B;	break;
	case 0xA:Temp = CC;	break;
	case 0xB:Temp = DP;	break;
	}
switch(DestReg)
	{
	case 0:	D = (u16)Temp;	break;
	case 1:	X = (u16)Temp;	break;
	case 2:	Y = (u16)Temp;	break;
	case 3:	U = (u16)Temp;	break;
	case 4:	S = (u16)Temp;	break;
	case 5:	PC = (u16)Temp;break;
	case 0x8:A = (u8)Temp;	break;
	case 0x9:B = (u8)Temp;	break;
	case 0xA:CC = (u8)Temp;	break;
	case 0xB:DP = (u8)Temp;	break;
	}
}
#undef CDEAD6809
